/**
 * 
 */
package com.shy.task.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.shy.base.service.impl.BaseServiceImpl;
import com.shy.task.dao.TaskEntityDao;
import com.shy.task.entity.TaskEntity;
import com.shy.task.quartz.core.QuartzService;
import com.shy.task.quartz.exception.QuartzJobException;
import com.shy.task.service.TaskEntityService;

/**
 * <p>Title:TaskEntityServiceImpl </p>
 * <p>Description: 任务信息 service层实现</p>
 * <p>Company:shy </p> 
 * @author H2013788
 * @date 2019年09月21日
 */
@Transactional
@Service("taskEntityService")
public class TaskEntityServiceImpl extends BaseServiceImpl<TaskEntity> implements TaskEntityService {
	
	/**
     * dao
     */
    @Resource
    private TaskEntityDao taskEntityDao;
    
    @Autowired
	private QuartzService quartzService;

    @Resource
    private void setBaseDao(TaskEntityDao taskEntityDao) {
        this.baseDao = taskEntityDao;
    }
    
    
    @Override
	@Transactional(rollbackFor = Exception.class)
	public void addJob(TaskEntity jobCfg) {
    	taskEntityDao.save(jobCfg);
		quartzService.addQuartzJob(jobCfg);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateJob(TaskEntity jobCfg) {
		taskEntityDao.update(jobCfg);
		//quartzService.updateQuartzJob(jobCfg);
		quartzService.updateQuartzJobParams(jobCfg);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteJob(Integer id) {
		TaskEntity jobCfg = taskEntityDao.getById(id);
		quartzService.deleteQuartzJob(jobCfg);
		taskEntityDao.deleteById(id);

	}

	@Override
	public List<TaskEntity> queryAllEnableJobCfgs() {
		return taskEntityDao.findByProperty("status", 1);
	}

	@Override
	public void updateQuartzExecuteClass(byte[] bytes, Integer id) {
		TaskEntity cfg = taskEntityDao.getById(id);
		if (null == cfg) {
			throw new QuartzJobException("[" + id + "]对应的job在数据库中不存在");
		}
		quartzService.updateQuartzJobExecuteClass(bytes,cfg);
	}


	@Override
	public void pauseQuartzJob(Integer id) {
		TaskEntity cfg = taskEntityDao.getById(id);
		cfg.setStatus(0);
		taskEntityDao.update(cfg);
		quartzService.pauseQuartzJob(getById(id));
	}


	@Override
	public void resumeQuartzJob(Integer id) {
		TaskEntity cfg = taskEntityDao.getById(id);
		cfg.setStatus(1);
		taskEntityDao.update(cfg);
		quartzService.resumeQuartzJob(getById(id));
	}


	@Override
	public void immediateExecuteJob(Integer id) {
		quartzService.immediateExecuteJob(getById(id));
	}

}
